package com.schooldate.model.action;

import com.alibaba.fastjson.JSON;
import com.schooldate.base.BaseAction;
import com.schooldate.model.entity.MessageBoard;
import com.schooldate.model.entity.PraiseRecord;
import com.schooldate.model.entity.UserInfo;
import com.schooldate.util.push.PushUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * 赞的Action
 * Created by etund on 2015/4/1 0001.
 */
@Controller
@Scope("prototype")
@SuppressWarnings("all")
public class PraiseRecordAction extends BaseAction<PraiseRecord> {

    private String uid;
    private String mid;


    /**
     * 赞
     * @throws Exception
     */
    public void praiseMessage() throws Exception{
        MessageBoard messageBoard = this.messageBoardService.getById(Long.parseLong(mid));
        messageBoard.setPraise(messageBoard.getPraise()+1);
        this.messageBoardService.update(messageBoard);
        model.setMessageBoardId(Long.parseLong(mid));
        model.setUserId(Long.parseLong(uid));
        model.setIsPraise(true);
        this.praiseRecordService.save(model);

        //        消息推送
        UserInfo reciUser = this.userInfoService.getById(messageBoard.getUserInfo().getId());
        PushUtils.pushToSingle(reciUser.getDevicetoken(), "你收到一条信息新");

        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
    }

    /**
     * 取消赞
     * @throws Exception
     */
    public void cancelPraise() throws Exception{
        MessageBoard messageBoard = this.messageBoardService.getById(Long.parseLong(mid));
        messageBoard.setPraise(messageBoard.getPraise() - 1);
        this.messageBoardService.update(messageBoard);
        PraiseRecord praiseRecord = this.praiseRecordService.getByUserIdAndMessageBoardId(Long.parseLong(uid),Long.parseLong(mid));
        praiseRecord.setIsPraise(false);
        this.praiseRecordService.update(praiseRecord);
        object.put("code",0);
        result = JSON.toJSONString(object);
        renderJson(result);
    }


    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getMid() {
        return mid;
    }

    public void setMid(String mid) {
        this.mid = mid;
    }
}
